package org.ghost.spring.cloud.hystrix.demo.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.ghost.spring.cloud.hystrix.demo.service.ITestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class TestServiceImpl implements ITestService {
    private final static Logger logger = LoggerFactory.getLogger(TestServiceImpl.class);

    @HystrixCommand(fallbackMethod = "fallbackProcess",
            commandProperties = {@HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE")})
    @Override
    public String process(String name) {
        logger.info("*****TestServiceImpl.process:" + name);
        long a = System.currentTimeMillis() % 2 == 0 ? 0 : 1;
        long b = System.currentTimeMillis() / a;
        return name;
    }

    public String fallbackProcess(String name) {
        logger.info("*****TestServiceImpl.fallbackProcess:" + name);
        return name + "->fallbackProcess";
    }

    @HystrixCommand(fallbackMethod = "fallbackProcess")
    @Override
    public String test(String name) {
        logger.info("*****TestServiceImpl.test:" + name);
        long a = System.currentTimeMillis() % 2 == 0 ? 0 : 1;
        long b = System.currentTimeMillis() / a;
        return name;
    }

    public String fallbackTest(String name) {
        logger.info("*****TestServiceImpl.fallbackTest:" + name);
        return name + "->fallbackTest";
    }
}
